package com.hwh.springdatajpademo.repository;

import com.hwh.springdatajpademo.pojo.User;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface UserRepository extends JpaRepository<User,String>, QuerydslPredicateExecutor<User> {
    List<User> findAllByNameLike(String name);

    @Query("from User where name = ?1 and email like email")
    List<User> gainUserListByUsernameOrEmail(String name,String email);

    @Query(nativeQuery = true,value = "SELECT\n" +
            "\t* \n" +
            "FROM\n" +
            "\t`user` u \n" +
            "WHERE\n" +
            "\tu.role_id = any(\n" +
            "\tSELECT\n" +
            "\t\trole.id \n" +
            "\tFROM\n" +
            "\t\trole \n" +
            "WHERE\n" +
            "\trole.role_name = ?1)")
    List<User> findUsersByRoleName(String roleName);

    List<User> findTop10By( Sort sort);

    @Modifying
    @Query("update User set name = ?2 , email = ?3 where id = ?1")
    int updateUser(String id,String name,String email);

    @Modifying
    @Query("delete from User where id = ?1")
    int deleteUser(String id);
}
